package com.caszx.chargingpile.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONException;
import com.app.base.app.BaseTopBarDelayActivity;
import com.app.utils.DiskUser;
import com.app.utils.SystemHelper;
import com.caszx.chargingpile.R;
import com.caszx.chargingpile.http.ConstantUrl;
import com.caszx.chargingpile.utils.SnackbarHelp;
import com.nispok.snackbar.Snackbar;

import org.json.JSONObject;

import java.util.HashMap;

import http.okhttp.OkHttpUtils;
import http.okhttp.callback.JsonCallback;
import okhttp3.Call;

public class ErrorActivity extends BaseTopBarDelayActivity {

    private  String  ordersId ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_error);
        setCenterTitle("电桩报错");
    }

    @Override
    protected void init() {
        ordersId = getIntent().getStringExtra("ID");
    }

    private TextView txt_server_time;
    private TextView btn_post;
    private EditText edit_phone;

    @Override
    protected void findView() {

        txt_server_time = (TextView) findViewById(R.id.txt_server_time);
        btn_post = (TextView) findViewById(R.id.btn_post);
        edit_phone = (EditText) findViewById(R.id.edit_phone);

        SpannableStringBuilder builder = new SpannableStringBuilder("客服热线 ");
        builder.append(getResources().getString(R.string.server_phone)).append("\n");
        builder.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorAccent)),0,builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.append("(服务时间 ");
        builder.append(getString(R.string.server_time)).append(")");
        txt_server_time.setText(builder);

        btn_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String content  = edit_phone.getText().toString().trim();
                if(TextUtils.isEmpty(content)){
                    SnackbarHelp.showSnackbarMsg(ErrorActivity.this,"请输入内容");
                    return;
                }

                postError(content);
            }
        });

    }


    private void postError(String content){
        SystemHelper.closeInputFromWindow(ErrorActivity.this);
        showProgressDialog("提交中");
        HashMap<String,String> params = new HashMap<>();
        params.put("token", DiskUser.getStringValueByKey(this,"token"));
        params.put("content",content);
        params.put("ordersId",ordersId);
        OkHttpUtils.post()
                .url(ConstantUrl.URL_ERROR_CHARGE)
                .params(params)
                .tag(this)
                .build()
                .execute(new JsonCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        dissmissProgressDialog();
                        SnackbarHelp.showSnackbarMsg(ErrorActivity.this,getString(R.string.error_network));
                        //showErrorView(R.string.error_network);
                    }

                    @Override
                    public void onResponse(JSONObject response) {
                        dissmissProgressDialog();
                        JSONObject resultJsonObject =  response.optJSONObject("result");
                        if(resultJsonObject!=null){
                            int code =  resultJsonObject.optInt("code");
                            if(code<0){
                                //error
                                // DialogHelp.showDialogOnlyMesg(resultJsonObject.optString("msg"),null);
                                //showErrorView(resultJsonObject.optString("msg"));
                                SnackbarHelp.showSnackbarMsg(ErrorActivity.this,resultJsonObject.optString("msg"));
                            }else{
                                try {
                                    SnackbarHelp.showSnackbarMsg(ErrorActivity.this,resultJsonObject.optString("msg"));

                                }catch (JSONException e){
                                    SnackbarHelp.showSnackbarMsg(ErrorActivity.this,getString(R.string.error_parser_json));
                                    //showErrorView(R.string.error_parser_json);
                                }
                            }
                        }
                    }
                });
    }
}
